<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Session;
class Product  extends Common
{
    
    

//以下是产品
    public function fistpage()
    {
        $column=column();
        $this->view->assign('column',$column);
        $column=Db::name('column');
        $class=$column->where('pid','0')->select();
        $this->view->assign('class',$class);
        return $this->view->fetch();
    }

    public function index()
    {
        $column=column();
        $this->view->assign('column',$column);
        $class=input('class');
        $this->view->assign('class',$class);
        return $this->view->fetch();
    }
    

    public function allproduct()
    {
        $class=input('class');
        if($class){
            $where['class']=$class;
        }
        //数量
        $leaveCount = Db::name("product")->where($where)->count();

        //分页数据
        $limit=input('limit');
        $page=input('page');
        $start=($page-1)*$limit;

        //调取数据
        $userall=Db::name("product")->where($where)->limit($start,$limit)->order('id desc')->select();

        //API数据格式处理
        $data= array();
        $data['code']=0;
        $data['msg']='';
        $data['count']=$leaveCount;
        $data['data']=$userall;

        //返回json数据
        echo $data=json_encode($data);
    }
    public function addproduct()
    {
        //编辑器图片上传api
        if(Request::instance()->isPost()){
            $file = request()->file();
            $fileinfo=$file['file']->getInfo(); 
            
            $fileurl=$fileinfo['tmp_name'];
            $filetype=explode('/',$fileinfo['type']);
            $filetype=$filetype[1];
            $filename=time().rand(0,9).'.'.$filetype;
            $copyurl='public/uploads/'.$filename;


            if(!copy($fileurl,$copyurl)){
                $fileinfo['error']=000000;
            }
            ;
          
            $imgrl = array();
            $imgrl['code']=$fileinfo['error'];
            $imgrl['msg']=00;
            $imgrl['data']['src']='/'.$copyurl;
            $imgrl['data']['tite']=$fileinfo['name'];

            $img=json_encode($imgrl);
            return $img;
        }
        //END

        $column=column(0);
        $this->view->assign('column', $column);
        return $this->view->fetch();
    }
    public function saveproduct()
    {
        $content=input('content');
        $datas=input('data');
        $datas=json_decode($datas,true);

        $save['class']=$datas['class'];
        $save['title']=$datas['title'];
        $save['issue']=$datas['issue'];
        $save['pay']=$datas['pay'];
        $save['content']=$content;
        $save['addtime']=date('Y-m-d H:i:s',time());
        
        $imglist='';
        $fist=1;
        for ($i=1; $i <= $datas['imgcount'] ; $i++) { 
            if($datas['imglist'.$i]){
                if($fist==1){
                    $imglist=$datas['imglist'.$i];
                    $fist++;
                }else{
                    $imglist=$imglist.'|'.$datas['imglist'.$i];
                }
            }
        }
        $save['imglist']=$imglist;

            $user=SESSION::get('cmuser');
            $log_title=$datas['title'];
            $log="add product ' $log_title '";
            set_log($user,$log);


        if($datas){
            $product=Db::name('product');
            $is_insert=$product->insert($save);
            if($is_insert){
                return '1';
            }else{
                return '99';
            }
        }else{
            return '参数错误';
        }
    }
    public function delspro()
    {
        $datas=input('data');
        $datas=json_decode($datas,true);

        $product=Db::name('product');



        foreach ($datas as $k => $v) {
            $del['id']= $v['id'];
            $product->where($del)->delete();
            $log_id=$log_id.'|'.$v['id'];
        }
            $user=SESSION::get('cmuser');
            $log_title=$log_id;
            $log="delete product ' $log_title '";
            set_log($user,$log);
        return '1';

    }
    public function editproduct()
    {
        $id=input('id');
        $product=Db::name('product');
        $info=$product->where(array('id' => $id))->find();
        $info['imglist']=explode('|',$info['imglist']);
        $info['num']=count($info['imglist']);
        $this->view->assign('info', $info);
        $column=column(0);
        $this->view->assign('column', $column);
        return $this->view->fetch();
    }
    public function editprosave()
    {
        $content=input('content');
        $datas=input('data');
        $datas=json_decode($datas,true);
        $where['id']=$datas['id'];
        $save['class']=$datas['class'];
        $save['issue']=$datas['issue'];
        $save['pay']=$datas['pay'];
        $save['content']=$content;
        $save['title']=$datas['title'];
        $save['updatetime']=date('Y-m-d H:i:s',time());
        $imglist='';
        $fist=1;
        for ($i=1; $i <= $datas['imgcount'] ; $i++) { 
            if($datas['imglist'.$i]){
                if($fist==1){
                    $imglist=$datas['imglist'.$i];
                    $fist++;
                }else{
                    $imglist=$imglist.'|'.$datas['imglist'.$i];
                }
            }
        }
        $save['imglist']=$imglist;
        
            $user=SESSION::get('cmuser');
            $log_title=$datas['title'];
            $log="edit product ' $log_title '";
            set_log($user,$log);

        
        if($datas['id']){
            $column=Db::name('product');
            $is_update=$column->where($where)->update($save);
            if($is_update){
                return '1';
            }else{
                return '99';
            }
        }else{
            return '参数错误';
        }
    }
    public function delpro()
    {
        $id=input('data');
        $product=Db::name('product');
        $check=$product->delete($id);

            $user=SESSION::get('cmuser');
            $log_title=$id;
            $log="delete product ' $log_title '";
            set_log($user,$log);


        if($check){
            return '1';
        }else{
            return '99';
        }
    }
    public function productimgsave()
    {
        if(Request::instance()->isPost()){
            $file = request()->file();
            $fileinfo=$file['file']->getInfo();
            
            $fileurl=$fileinfo['tmp_name'];
            $filetype=explode('/',$fileinfo['type']);
            $filetype=$filetype[1];
            $filename=time().rand(0,9).'.'.$filetype;
            $copyurl='public/uploads/'.$filename;


            if(!copy($fileurl,$copyurl)){
                $fileinfo['error']=000000;
            };
          
            $imgrl = array();
            $imgrl['code']=$fileinfo['error'];
            $imgrl['msg']=00;
            $imgrl['data']['src']='/'.$copyurl;
            $imgrl['data']['tite']=$fileinfo['name'];

            $img=json_encode($imgrl);
            return $img;
        }
    }

 
//栏目
    public function column(){
        
        $column=column(0);
        $this->view->assign('column', $column);
        
        return $this->view->fetch();
    }
    public function allcolumn()
    {
        //数量
        $leaveCount = Db::name("column")->count();

        //分页数据
        $limit=input('limit');
        $page=input('page');
        $start=($page-1)*$limit;

        //调取数据
        $all=column(0);
        
        $i=0;
        foreach ($all as $k => $v) {
            if($i>=$start &&  $i<$start+$limit){
                $alls[]=$all[$k];
            }
            $i++;
        }

        //API数据格式处理
        $data= array();
        $data['code']=0;
        $data['msg']='';
        $data['count']=$leaveCount;
        $data['data']=$alls;

        //返回json数据
        echo $data=json_encode($data);
    }
    public function addcolumn()
    {
        $data=input('data');
        $data=json_decode($data,true);
        $where['id']=$data['pid'];
        $info=Db::name('column')->where($where)->find();

        $data['level']=$info['level']+1;

            $user=SESSION::get('cmuser');
            $log_title=$data['classname'];
            $log="add column ' $log_title '";
            set_log($user,$log);

        if(Db::name('column') -> insert($data)){
            return '1';
        }else{
            return '99';
        }

    }
    public function delcolumns(){
        $data=input('data');
        $data=json_decode($data,true);

        foreach ($data as $k => $v) {
            $del['id']=$v['id'];
            Db::name('column')->where($del)->delete();
            $log_id=$log_id.'|'.$v['id'];
        }
            $user=SESSION::get('cmuser');
            $log_title=$log_id;
            $log="delete column ' $log_title '";
            set_log($user,$log);


        return '1';
    }


    public function delcolumn(){
        $data=input('data');
        $del['id']=$data;
        
        $user=SESSION::get('cmuser');
            $log_title=$del['id'];
            $log="delete column ' $log_title '";
            set_log($user,$log);

        if(Db::name('column')->where($del)->delete()){
            return '1';
        }else{
            return '99';
        }
        
    }
    public function editcolumn(){

        //编辑器图片上传api
        if(Request::instance()->isPost()){
            $file = request()->file();
            $fileinfo=$file['file']->getInfo(); 
            
            $fileurl=$fileinfo['tmp_name'];
            $filetype=explode('/',$fileinfo['type']);
            $filetype=$filetype[1];
            $filename=time().rand(0,9).'.'.$filetype;
            $copyurl='public/uploads/'.$filename;


            if(!copy($fileurl,$copyurl)){
                $fileinfo['error']=000000;
            }
            ;
          
            $imgrl = array();
            $imgrl['code']=$fileinfo['error'];
            $imgrl['msg']=00;
            $imgrl['data']['src']='/'.$copyurl;
            $imgrl['data']['tite']=$fileinfo['name'];

            $img=json_encode($imgrl);
            return $img;
        }
        //END

        $id=input('id');
        if(!$id){
            return '参数错误!';
        }
        $where['id']=$id;
        $info=Db::name('column')->where($where)->find();
        if(!$info){
            return '栏目已删除!';
        }
        $this->view->assign('info', $info);

        //dump($info);
        $column=column(0);
        $this->view->assign('column', $column);

        
        return $this->view->fetch();

    }
    public function savecolumn(){
        $content=input('content');
        $datas=input('data');
        $datas=json_decode($datas,true);
        $where['id']=$datas['id'];
        $save['pid']=$datas['pid'];
        $save['classname']=$datas['classname'];
        $save['content']=$content;
        $save['type']=$datas['type'];

            $user=SESSION::get('cmuser');
            $log_title=$save['classname'];
            $log="edit column ' $log_title '";
            set_log($user,$log);


        if($datas['id']){
            $column=Db::name('column');
            $is_update=$column->where($where)->update($save);
            if($is_update){
                return '1';
            }else{
                return '99';
            }
        }else{
            return '参数错误';
        }
        
    }















}
